﻿using BLL;
using BLL.教师操作BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace NCRE学生考试端V1._0
{
    public partial class frmAddStudent : Form
    {
        public frmAddStudent()
        {
            InitializeComponent();
        }

        #region 窗体单一显示-李金颖-2018-12-8 12:02:04
        private static frmAddStudent FrmAddStudent = null;
        public static frmAddStudent GetInstance()
        {
            if (FrmAddStudent==null|| FrmAddStudent.IsDisposed)
            {
                FrmAddStudent = new frmAddStudent();
            }
            return FrmAddStudent;
        }
        #endregion

        AddStudentBLL addstudent = new AddStudentBLL();
        DataTable dt_College = null;

        StudentScoreBLL studentScore = new StudentScoreBLL();
        StudentInfoEntity studentInfo = new StudentInfoEntity();
        
        TimeEntity timeEntity = new TimeEntity();
      

        private void frmAddStudent_Load(object sender, EventArgs e)
        {
            //获取下拉框学院信息
            dt_College = new DataTable();
            dt_College = addstudent.getCollegeInfo();
            List<String> list = new List<string>();
            foreach (DataRow item in dt_College.Rows)
            {
                list.Add(item["CollegeName"].ToString());
            }
            if (dt_College!=null && dt_College.Rows.Count > 0)
            {
                cobCollege.DataSource = list;  
            }
           
            //选定默认性别为男
            rbMale.Checked = true;

            

            //IP下拉框中加载信息            
            DataTable dt_IP = new DataTable();
            dt_IP = addstudent.SelectIP();
            List<String> Iplist = new List<string>();
            foreach (DataRow item in dt_IP.Rows)
            {
                Iplist.Add(item["ipName"].ToString());
            }
            if (dt_IP != null && dt_IP.Rows.Count > 0)
            {
                cboExamPlace.DataSource = Iplist;
            }
        }

        

        private void btnOK_Click(object sender, EventArgs e)
        {
            //申明实例
            StudentInfoBll studentBll = new StudentInfoBll();
            StudentInfoEntity studentInfo = new StudentInfoEntity();
            //学生学号
            studentInfo.studentID = txtStudentNo.Text.ToString().Trim();
            //学生姓名
            studentInfo.studentName = txtStudentName.Text.ToString().Trim();
            //学生的学院
            DataRow[] drs_college =  dt_College.Select("CollegeName='"+ cobCollege.Text.ToString().Trim() +"'");
            foreach (DataRow item in drs_college)
            {
                studentInfo.CollegeID = item["CollegeID"].ToString();
            }
            //性别
            if (rbFemale.Checked == true)
            {
                studentInfo.sex = rbFemale.Text.ToString();

            }
            else {
                studentInfo.sex = rbMale.Text.ToString();
            }
           
            studentInfo.major = listMajor.Text.ToString();
            studentInfo.majorClass = txtClasses.Text.ToString();
            studentInfo.grade = txtGrade.Text.ToString();
            studentInfo.examPlaceID = cboExamPlace.Text.ToString();           
            StudentInfoEntity studentExist =  studentBll.GetStudentById(studentInfo.studentID);
            if (studentExist != null && studentExist.studentID != string.Empty)
            {
                MessageBox.Show(this, "该学生已存在", "提示", MessageBoxButtons.OK);

                //清空学号框，光标定位学号框-白爱民-2018-12-8 11:33:52
                txtStudentNo.Text = ""; //清空学号框
                txtStudentNo.Focus(); //光标定位学号框
            }
            else if (txtStudentName.Text == "" || txtStudentNo.Text == "" || txtGrade.Text == "" || txtClasses.Text == "")
            {
                MessageBox.Show(this, "学生信息未输入完整！！", "提示", MessageBoxButtons.OK);
            }
            else 
            {                
                bool flag_Add = addstudent.addStudentInfo(studentInfo);
                if (flag_Add)
                {
                 
                    MessageBox.Show("添加学生信息成功");

                    //修改人：白爱民-获取学号焦点并清空学号和姓名文本框-2018-12-8 11:33:52
                    txtStudentNo.Text = ""; //清空学号框
                    txtStudentName.Text = ""; //清空姓名框
                    txtStudentNo.Focus(); //光标定位学号框
                }
            }
           

   

        }

        private void cobCollege_SelectedIndexChanged(object sender, EventArgs e)
        {
            //获取下拉框专业信息          
            timeEntity.CollegeName = cobCollege.Text.Trim();
            DataTable dt_Majot = new DataTable();
            dt_Majot = addstudent.SelectMajorByCollegeName(timeEntity);
            List<String> majorlist = new List<string>();
            foreach (DataRow item in dt_Majot.Rows)
            {
                majorlist.Add(item["Major"].ToString());
            }
            if (dt_Majot != null && dt_Majot.Rows.Count > 0)
            {                
                listMajor.DataSource = majorlist;
            }
        }
           


        /// <summary>
        /// 学号文本框只能输入数字-白爱民-2018-12-8 11:39:52
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtStudentNo_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                e.Handled = true;//经判断为数字，可以输入
            }
            else
            {
                e.Handled = false; 
            }
        }
        #region 去掉边框显示-李金颖-2018-12-8 14:31:59
        private void groupBox1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.Clear(this.BackColor);
        }

        private void groupBox2_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.Clear(this.BackColor);
        }
        #endregion


    }
}
